#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Copyright © 2014, Finium Sdn Bhd, All Rights Reserved
 * 
 * EmailServiceImpl.java
 * Modification History
 * *************************************************************
 * Date				Author						Comment
 * May 28, 2012		Venkaiah Chowdary Koneru	Created
 * *************************************************************
 */
package ${package}.commons.service;

import java.util.List;

import org.springframework.util.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Implementation of Email sending service
 * 
 * @author Venkaiah Chowdary Koneru
 * 
 */
@Component
public class EmailServiceImpl implements EmailService
{
    private static final Log LOG = LogFactory.getLog(EmailServiceImpl.class);

    @Value("${symbol_pound}{appProps['mail.server']}")
    private String mailServer;

    @Value("${symbol_pound}{appProps['mail.port']}")
    private int mailPort;

    @Value("${symbol_pound}{appProps['mail.starttls']}")
    private boolean tls;

    @Value("${symbol_pound}{appProps['mail.login.id']}")
    private String mailUser;

    @Value("${symbol_pound}{appProps['mail.password']}")
    private String mailPassword;

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendEmail(String content, String subject, String toUserEmail,
            String fromUserEmail) throws EmailException
    {
        HtmlEmail email = sendHtmlEmailInternal(content, subject, toUserEmail,
                fromUserEmail);

        email.send();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendEmail(String content, String subject,
            List<String> toUserEmails, String fromUserEmail)
            throws EmailException
    {
        if (toUserEmails != null)
        {
            for (String toUserEmail : toUserEmails)
            {
                HtmlEmail email = sendHtmlEmailInternal(content, subject,
                        toUserEmail, fromUserEmail);

                email.send();
            }
        }
    }

    /**
     * internal utility method to create instance of HTML email object
     * 
     * @param content
     *            email content to be send
     * @param subject
     *            email subject
     * @param toUserEmail
     *            recipient email
     * @param fromUserEmail
     *            can be null
     * @return HTML Email object instance
     * @throws EmailException
     *             in case of errors
     */
    private HtmlEmail sendHtmlEmailInternal(String content, String subject,
            String toUserEmail, String fromUserEmail) throws EmailException
    {
        HtmlEmail email = new HtmlEmail();

        if (LOG.isDebugEnabled())
        {
            LOG.debug("Setting email property debug to true");
            email.setDebug(true);

            LOG.debug("Mail Server: " + mailServer);
            LOG.debug("Mail Port: " + mailPort);
            LOG.debug("use tls: " + tls);

            LOG.debug("Mail Subject: " + subject);
            LOG.debug("Mail Content: " + content);
        }

        email.setHostName(mailServer);
        email.setSmtpPort(mailPort);
        email.setTLS(tls);

        if (StringUtils.hasText(mailUser) && !StringUtils.hasText(mailPassword))
        {
            email.setAuthentication(mailUser, mailPassword);
        }

        email.addTo(toUserEmail);

        if (StringUtils.hasText(fromUserEmail))
        {
            email.setFrom(fromUserEmail);
        }
        else if (StringUtils.hasText(mailUser))
        {
            email.setFrom(mailUser);
        }

        email.setSubject(subject);

        email.setHtmlMsg(content);

        return email;
    }
}
